package com.mms.imagegenius;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.mms.imagegenius.datastructure.CompositePicture;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class GalleryActivity extends Activity implements OnClickListener {

	HorizontalScrollView hsvForeground, hsvBackground;
	LinearLayout llForeground, llBackground;
	private static final String SELECTED_TAG = "S";
	HashMap<ImageView, Boolean> selectedImageMap;
	ActionBar aBar;
	
	private static final int imageViewWidth = 300;
	private static final int imageViewHeight = 300;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Create another thread to populate the layouts
		// TODO change all Bitmap to Picture (type)
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gallery);
		
		//get the ActionBar
		aBar = this.getActionBar();
		aBar.setDisplayHomeAsUpEnabled(true);
		
		selectedImageMap = new HashMap<ImageView, Boolean>();

		hsvForeground = (HorizontalScrollView) findViewById(R.id.hsvForeground);
		hsvBackground = (HorizontalScrollView) findViewById(R.id.hsvBackground);

		llForeground = (LinearLayout) findViewById(R.id.llForegroundGal);
		llBackground = (LinearLayout) findViewById(R.id.llBackgroundGal);

		// TODO change this
		int[] foregroundImageID = { R.drawable.ic_launcher, R.drawable.gal_test_obj1, R.drawable.gal_test_img1 };
		int[] backgroundImageID = { R.drawable.ic_launcher, R.drawable.gal_test_img1, R.drawable.gal_test_img2, R.drawable.gal_test_img3 };

		// init foreground gallery layout
		initLayout(llForeground, foregroundImageID);

		// init background gallery layout
		initLayout(llBackground, backgroundImageID);

		//Log.d("Gallery Activity", "GalleryActivity.onCreate() init adapters");
		// galForeground.setAdapter(new GalleryImageAdapter(this,
		// foregroundImageID));
		// galBackground.setAdapter(new GalleryImageAdapter(this,
		// backgroundImageID));

		// galForeground.setOnItemSelectedListener(this);
		// galBackground.setOnItemSelectedListener(this);

		Log.d("Gallery Activity", "GalleryActivity.onCreate() finished.");
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int id = item.getItemId();
		if(id == android.R.id.home){
			Log.d("Gallery Activity", "Go back");
			
			Intent goBack = new Intent(this, MainActivity.class);
			goBack.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(goBack);
		}else if(id == R.id.miCombine){
			//combine the Pictures
			Log.d("Gallery Activity", "Combine Pictures");
		}else if(id == R.id.miSwitch){
			//switch obj/background property of Picture
			Log.d("Gallery Activity", "Switch Obj/Background");
		}else if(id == R.id.miDelete){
			//delete the selected Picture(s)
			Log.d("Gallery Activity", "Delete selected picture(s)");
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.activity_gallery, menu);
	    return true;
	}

	private void initLayout(ViewGroup parentView, int[] resID) {
		for (int i = 0; i < 5; i++)
			for (int img = 0; img < resID.length; img++) {
				LayoutInflater inflater = (LayoutInflater) llForeground.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				inflater.inflate(R.layout.gallery_item, null);
				GalleryItem item = new GalleryItem(llForeground, resID[img]);
				View currView = inflater.inflate(R.layout.gallery_item, null);
				
				ImageView iv = (ImageView) currView.findViewById(R.id.ivGalItem);
				LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(GalleryActivity.imageViewWidth, GalleryActivity.imageViewHeight);
				iv.setLayoutParams(layoutParams);
				
				//MMSPicture pic = new MMSPicture(item.getIconImg()); //TODO change this to a file source
				CompositePicture picture = new CompositePicture(item.getIconImg(), item.getIconImg());
//				pic.setBackground(item.getIconImg()); //TODO make a new standard background for the Gallery
				iv.setImageBitmap(picture.getOverlayedPicture());
				drawImageViewBorder(iv, false);
				iv.setOnClickListener(this);
				parentView.addView(currView);
			}
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		Log.d("Gallery Activity", "Selected Image = " + arg0.toString());

		/*
		 * int size = gal.getCount(); for (int pic = 0; pic < size; pic++) {
		 * ImageView imageView = (ImageView) layout.getChildAt(pic);
		 * imageView.setBackgroundDrawable
		 * (getResources().getDrawable(R.drawable.gal_image_border));
		 * imageView.setPadding(3, 3, 3, 3); }
		 */

		// ImageView imageView = (ImageView) layout.getChildAt(selectedImage);
		ImageView imageView = (ImageView) arg0;

		if (imageView.getTag() != null && imageView.getTag().equals(SELECTED_TAG)) {
			// this picture is already selected
			drawImageViewBorder(imageView, false);
		} else {
			// this picture is not yet selected
			drawImageViewBorder(imageView, true);
		}
	}

	private void drawImageViewBorder(ImageView imageView, boolean selected) {
		if (selected) {
			imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.gal_image_selected_border));
			imageView.setPadding(10, 10, 10, 10);
			imageView.setTag(SELECTED_TAG);
			selectedImageMap.put(imageView, true);
		} else {
			imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.gal_image_border));
			imageView.setPadding(10, 10, 10, 10);
			imageView.setTag(null);
			selectedImageMap.put(imageView, false);
		}
	}

	ArrayList<Bitmap> getAllSelectedBitmaps() {
		//should in the future be getAllSelectedPictures
		if (selectedImageMap.isEmpty()) {
			return null;
		}
		ArrayList<Bitmap> selected = new ArrayList<Bitmap>();
		Set<ImageView> keys = selectedImageMap.keySet();
		Iterator<ImageView> iterator = keys.iterator();
		while (iterator.hasNext()) {
			ImageView iv = iterator.next();
			if (iv.getTag() != null && iv.getTag().equals(SELECTED_TAG)) {
				selected.add(((BitmapDrawable) iv.getDrawable()).getBitmap());
			}
		}
		return selected;
	}

}
